package com.scu.bank.web;
import com.scu.bank.core.jo.Result;
import com.scu.bank.core.jo.ResultGenerator;
import com.scu.bank.model.pojo.ScienceDynamic;
import com.scu.bank.service.ScienceDynamicService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
* Created by pmeng on 2021/01/05.
*/
@RestController
@RequestMapping("/science/dynamic")
public class ScienceDynamicController {
    @Autowired
    private ScienceDynamicService scienceDynamicService;

    @PostMapping("/add")
    public Result add(@RequestBody ScienceDynamic scienceDynamic) {
        scienceDynamicService.addScienceDynamic(scienceDynamic);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/delete")
    public Result delete(@RequestBody List<String> ids) {
        if (ids == null){
            return ResultGenerator.genFailResult("请至少选择一项数据!");
        }
        scienceDynamicService.deleteByIds(ids);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    public Result update(@RequestBody ScienceDynamic scienceDynamic) {
        scienceDynamicService.update(scienceDynamic);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/detail")
    public Result detail(@RequestParam String id) {
        ScienceDynamic scienceDynamic = scienceDynamicService.getById(id);
        return ResultGenerator.genSuccessResult(scienceDynamic);
    }

    @PostMapping("/list")
    public Result list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size) {
        PageInfo<ScienceDynamic> pageInfo = scienceDynamicService.list(page, size);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    /**
     * 科研动态的上下架
     * @param id
     * @return
     */
    @PostMapping("/targetScienceDynamic")
    public Result targetScienceDynamic(@RequestParam(required = true) String id) {
        scienceDynamicService.targetScienceDynamic(id);
        return ResultGenerator.genSuccessResult();
    }

    /**
     * 获取最新的科研动态
     * @return
     */
    @GetMapping("/getScienceDynamic")
    public Result getScienceDynamic() {
        List<ScienceDynamic> pageInfo = scienceDynamicService.getScienceDynamic();
        return ResultGenerator.genSuccessResult(pageInfo);
    }
}
